﻿/// <reference path="~/content/scripts/dynatree" />
/// <reference path="~/content/scripts/contextMenu" />
/// <reference path="~/content/scripts/application" />
new Module('CobraCms.Js.ContentTree', function () {
    var Class = Phosphor.Utilities.Class;

    var TreeUi = new Class({

        TreeContainer: {},
        Tree: {},

        initialize: function (options) {
            this.TreeContainer = $(options);
            this.CreateTree();
        },

        CreateTreeSucess: function (data) {
            var rootJsTreeNode = CobraCms.Js.ContentTreeUtilities.BuildJsTreeNode(data);
            this.TreeContainer.dynatree({
                persist: true,
                onCreate: this.CreateContextMenu.bind(this),
                onClick: this.JsNodeClicked.bind(this),
                onLazyRead: this.ReloadJsNode.bind(this),
                children: rootJsTreeNode
            });
            this.Tree = this.TreeContainer.dynatree("getTree");
        },

        CreateTreeError: function (jqHR, textStatus, errorThrown) {
            alert("Failed: " + textStatus);
        },


        CreateTree: function () {
            var url = '/ajax/content/tree/nodes/root';
            $.ajax(
                {
                    type: "GET",
                    url: url,
                    success: this.CreateTreeSucess.bind(this),
                    error: this.CreateTreeError.bind(this)
                });
        },

        ReloadJsNode: function (jsTreeNode) {
            new CobraCms.Js.ContentTreeNode(jsTreeNode).Reload();
        },

        JsNodeClicked: function (jsTreeNode, event) {
            if (jsTreeNode.data.contentNode.NodeId) {
                if (jsTreeNode.getEventTargetType(event) == "title") {
                    CobraCms.Js.Application.Instance.OpenActionDialog('/content/nodes/' + jsTreeNode.data.contentNode.NodeId);
                    return false; // Prevent default processing
                }
            }
        },


        AddTreeAction: function (action) {
            return (function (menuItem, menu) {
                CobraCms.Js.Application.Instance.InitiateTreeAction(action.contentNode, action);
            });
        },

        CreateContextMenu: function (jsTreeNode) {
            var menu = [];
            var contentNode = jsTreeNode.data.contentNode;
            if (contentNode.Actions) {
                contentNode.Actions.forEach(function (action, index) {
                    var contextItem = {};
                    contextItem[action.Name] = {
                        onclick: function (menuItem, menu) {
                            CobraCms.Js.Application.Instance.InitiateTreeAction(jsTreeNode, action);
                        }
                    };
                    menu.push(contextItem);
                });
            }
            $(jsTreeNode.span).contextMenu(menu, { theme: 'vista' });
        },

        ReloadNode: function (nodeId) {
            var node = this.Tree.getNodeByKey(nodeId);
            node.makeVisible();
            node.reloadChildren(function (node, isOk) {
                if (!isOk) alert("Node " + node + " could not be reloaded.");
            });
        }
    });

    this.TreeUi = TreeUi;

    this.Instance = {};

    $(".content-tree-container").each(function () {

        CobraCms.Js.ContentTree.Instance = new CobraCms.Js.ContentTree.TreeUi(this);
    });

});